﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MiserBlogData.Entities;
using MiserDB.DataHelp;
using log4net;
using MiserBlogHelper.Helper;
using NHibernate.Criterion;
using MiserBlogMVC.Models;
using MiserBlogData.Entities.Type;

namespace MiserBlogMVC.Controllers
{
    [MiserBlogMVC.Filter.AuthorVisitorFilter]
    public class ArticleController : Controller
    {
         
        public ActionResult Index(int? id, int? page)
        {

            if (HttpContext.User.Identity is VisitorIdentity && ((VisitorIdentity)HttpContext.User.Identity).Person is Visitor)
            {
                Visitor visitor = (Visitor)((VisitorIdentity)HttpContext.User.Identity).Person;
            }


            if (id.HasValue)
            {
                using (MiserDB.DataHelp.ActionTranHelper data = new MiserDB.DataHelp.ActionTranHelper())
                {
                    Article artice = data.GetData<Article>(id.Value);
                    HttpContextHelper help = new HttpContextHelper(HttpContext);


                    List<MiserDB.DataHelp.QueryHelper.WhereKey> whereKeyTag = new List<MiserDB.DataHelp.QueryHelper.WhereKey>();
                    whereKeyTag.Add(new MiserDB.DataHelp.QueryHelper.WhereKey("Articles", "article"));
                    whereKeyTag.Add(new MiserDB.DataHelp.QueryHelper.WhereKey("User", "user"));
                    List<SimpleExpression> whereItemTag = new List<SimpleExpression>();
                    whereItemTag.Add(Restrictions.Eq("user.Id", SiteAdmin.Id));
                    whereItemTag.Add(Restrictions.Eq("article.Id", id.Value));
                    whereItemTag.Add(Restrictions.Eq("State", (short)DataState.Release));

                    artice.Tags = data.GetList<Tag>(whereKeyTag, whereItemTag, null);

                    if (HttpContext.Request.HttpMethod == "POST")
                    {
                        Comment comment = new Comment();
                        comment.Id = 0;
                        comment.Article = artice;
                        comment.CDate = DateTime.Now;
                        comment.Content = HttpContext.Request["txtComment"];
                        comment.User = A_Person.CreateWithCookie();
                        data.Save(comment);
                    }
                    else
                    {
                        artice.Visits++;
                    }
                    data.SaveOrUpdate(artice);



                    //Dictionary<string, List<SimpleExpression>> where = new Dictionary<string, List<SimpleExpression>>();
                    //List<SimpleExpression> whereItem = new List<SimpleExpression>();
                    //whereItem.Add(Restrictions.Eq("Id",id.Value));
                    //where.Add("Article",whereItem);


                    List<MiserDB.DataHelp.QueryHelper.WhereKey> whereKey = new List<MiserDB.DataHelp.QueryHelper.WhereKey>();
                    whereKey.Add(new MiserDB.DataHelp.QueryHelper.WhereKey("Article", "article"));

                    List<SimpleExpression> whereItem = new List<SimpleExpression>();
                    whereItem.Add(Restrictions.Eq("article.Id", id.Value));

                    artice.CommentList = data.GetList<Comment>(whereKey, whereItem, help);
                    artice.CommentList = artice.CommentList.OrderByDescending<Comment, int>(r => r.Id).ToList();

                    return View(artice);
                }
            }
            return RedirectToRoute(new { controller = "Home", action = "Index" });
        }
    }
}
